import fs from "fs"
import path from "path"
import { exec } from "child_process"

/**
 * exec_command
 *
 * @param {string} command -
 */
function exec_command(command) {
    exec(command, (error, stdout, stderr) => {
        if (error) {
            console.error(`error {{-----------------------------------------------------------\n`)
            console.error(`command: ${command}\n`)
            console.error(`${error}`)
            console.error(`-----------------------------------------------------------}} error\n`)
            return
        }
        if (stderr) {
            // console.warn(`stderr {{-----------------------------------------------------------\n`)
            console.warn(`${stderr}`)
            // console.warn(`-----------------------------------------------------------}} stderr\n`)
            // return
        }
        if (stdout) {
            // console.log(`\x1b[32mstdout {{-----------------------------------------------------------\x1b[0m\n`)
            console.log(`\x1b[32mcommand: ${command}\x1b[0m\n`)
            console.log(`\x1b[32m${stdout}\x1b[0m`)
            // console.log(`\x1b[32m-----------------------------------------------------------}} stdout\x1b[0m\n`)
        }
    })
}

// waiting": "node -e \"setTimeout(() => { process.exit(0); }, 1000);\"",
function waiting() {
    setTimeout(() => {
        // process.exit(0)
    }, 1000)
}

function _finish_release() {
    //
    const root = process.cwd()
    const packageJsonPath = path.resolve(root, "package.json")
    fs.readFile(packageJsonPath, "utf8", (_, data) => {
        _process_data(data)
    })

    /**
     *
     * @param {string} data
     */
    function _process_data(data) {
        //
        try {
            console.log(`\n`)
            console.log(`\x1b[32m正在发布，请耐心等待 .... \x1b[0m\n`)
            const packageJson = JSON.parse(data)
            const _version = `v${packageJson.version}`
            const _message_t = `chore(release): ${_version} 🏆`

            const _command1 = `git commit -m "${_message_t}" --allow-empty`
            const _command2 = `git tag ${_version} -m "${_message_t}" --force`
            const _command3 = `git push --follow-tags origin master`

            // console.log(`\x1b[32mcommand: ${_command1}\x1b[0m\n`)
            exec(_command1, (error, stdout, stderr) => {
                if (stderr) {
                    console.warn(stderr)
                }
                if (stdout) {
                    console.log(`\x1b[32m${stdout}\x1b[0m`)
                }
                exec(_command2, (error, stdout, stderr) => {
                    if (stderr) {
                        console.warn(stderr)
                    }
                    if (stdout) {
                        console.log(`\x1b[32m${stdout}\x1b[0m`)
                    }
                    exec(_command3, (error, stdout, stderr) => {})
                    if (stderr) {
                        console.warn(stderr)
                    }
                    if (stdout) {
                        console.log(`\x1b[32m${stdout}\x1b[0m`)
                    }
                    console.log(`\x1b[32m发布成功！\x1b[0m\n`)
                })
            })
        } catch (error) {
            console.log(error)
        }
    }
}

export { _finish_release as finish_release }
